/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SaleManager.Category.Logic;

import Data.DAO.IDataStore;
import data.DAO.ICategoryStore;
import data.pojo.Category;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 *
 * @author Kite
 */
public class RemoveCategoryManage {
    
    private List<Category> searchList = new ArrayList<Category>();
    private List<Category> removeList = new ArrayList<Category>();
    private ArrayList<Integer> indexes = new ArrayList<Integer>();
    private IDataStore categoryDS = new ICategoryStore();   
    private List<Category> allCategories;
    
    public RemoveCategoryManage() {
        allCategories = (ArrayList<Category>) categoryDS.getObjects();
    }
    
    
    
    public Vector<List> searchTableVector(String key, String keywords) {
        searchList = categoryDS.searchObjectsByAttr(key, ".*" + keywords + ".*");
        return new Vector<List>(toCategoryTable(searchList));
    }
    
     public Vector<List> removeTableVector() {
        return new Vector<List>(toCategoryTable(removeList));
    }
     
     
    public boolean addToRemoveList(int index,Category c) {
        if (this.isValid(index)) {
            this.indexes.add(index);
            this.removeList.add(c);
            return true;
        } else {
            return false;
        }
    }
       
       
       
    private boolean isValid(int index) {
        for (Integer in : indexes) {
            if (in==index) {
                return false;
            }
        }
        return true;
    }
    
        public int getIndexInAll(Category c) {
        for(int i=0; i<allCategories.size(); i++){
            if(allCategories.get(i).getName().equals(c.getName())){
                return i;
            }
        }
        return -1;
    }
    
    public Category getBySearch(int index) {
        return this.searchList.get(index);
    }
    
    public void deleteFromRemList(int index) {
        this.removeList.remove(index);
        this.indexes.remove(index);
    }

    
    public boolean commitRemList() {
         for(Category book: removeList){
            for(int i=0; i<allCategories.size(); i++){
                if(book.getName().equals(allCategories.get(i).getName())){
                    allCategories.remove(i);
                }
            }
        }
        categoryDS.update(allCategories);
        removeList = new ArrayList<Category>();
        return true;
    }
    
    
        public Vector<Vector<String>> toCategoryTable(List list) {
        try {
            Vector<Category> seList = new Vector(list);
            Vector<Vector<String>> reList = new Vector<Vector<String>>();
            for (int i = 0; i < seList.size(); i++) {
                reList.add((Vector<String>) toCategoryStringArray(seList.get(i)));
            }
            return reList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List toCategoryStringArray(Category category) {
        Vector<String> list = new Vector<String>();
        list.add(category.getName());//name
        return list;
    }
    
}
